//
//  GameRecommandCell.swift
//  appstore
//
//  Created by llbt-sk on 2019/11/7.
//  Copyright © 2019 Roy. All rights reserved.
//

import UIKit

class GameRecommandCell: UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate {
    
    let RecommandCellIdentifier = "RecommandCellIdentifier"
    
    lazy var flowLayout:CommonCollectionFlowLayout = {
        let flowLayout = CommonCollectionFlowLayout()
        flowLayout.minimumLineSpacing = 10.0
        flowLayout.scrollDirection = .horizontal
        flowLayout.sectionInset = UIEdgeInsets(top: 0.0, left: 20.0, bottom: 0.0, right: 20.0)
        flowLayout.itemSize = CGSize(width: ScreenWidth - 40.0, height: 336.0)
        return flowLayout
    }()
    
    var collectionView:UICollectionView!
    
    // 数据
    let data:[GameRecommandModel] = [
        GameRecommandModel(feature: "MAJOR UPDATE", name: "Onmyoji", desc: "Cards", coverImageName: "cover_1"),
        GameRecommandModel(feature: "NEW GAME", name: "Clash Royale", desc: "Strategy", coverImageName: "cover_2"),
        GameRecommandModel(feature: "REDISCOVER THIS", name: "Fantasy Westward Journey", desc: "Adventure", coverImageName: "cover_3")
    ]
    
    var selectItemIndex:((_ index:Int) -> (Void))?
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.initView()
    }
    
    func initView() -> Void {
        
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout)
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.backgroundColor = .white
        collectionView.showsHorizontalScrollIndicator = false
        self.contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalTo(0.0)
        }
        collectionView.register(RecommandCollectionCell.self, forCellWithReuseIdentifier: RecommandCellIdentifier)
    }
    
    // UICollectionViewDataSource
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return data.count
    }
    
    // UICollectionViewDelegate
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: RecommandCellIdentifier, for: indexPath) as! RecommandCollectionCell
        cell.model = data[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectItemIndex!(indexPath.row)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
